package com.wantup.springboot.boot.task;

import com.wantup.springboot.boot.beans.TxPerson;
import com.wantup.springboot.boot.service.TxPersonService;
import com.wantup.springboot.boot.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.CannotCreateTransactionException;
import org.springframework.transaction.TransactionException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
  * @author:  zhaoluowei
  * @date :   2018/11/7 13:03
  * @version  1.0
  */
//@Component
public class TimeTask {

    @Autowired
    public TxPersonService txPersonService;


    /**
     *
     * @Scheduled
     * @EnableScheduling
     * 属于定时任务的串行方式
     * 定时器 启动就会执行 定时任务 需要调用
     *
     * http://cron.qqe2.com/
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void sum() throws Exception {


        try{

            System.out.println(" 定时器开始执行 : "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

            //包左不包右
            int anInt = new Random().nextInt(100);
            int gender = new Random().nextInt(2);

            TxPerson txPerson = new TxPerson();
            txPerson.setUsername("damimi - "+anInt);
            txPerson.setPassword("123123 - "+anInt);
            txPerson.setAddr("爱上了大会上的风格VB味儿 - "+anInt);
            txPerson.setGender(gender);
            txPerson.setBirth(new Date());
            txPerson.setPname("啦啦啦 - "+anInt);

            txPersonService.addTxPerson(txPerson);

            int countItem = txPersonService.getCountItem();
            System.out.println("共有表数据 : "+countItem);

        }catch(CannotCreateTransactionException c){
            System.out.println(c);

        }catch(TransactionException t){
            System.out.println(t);

        }catch (Exception e){
            System.out.println(e);
        }




    }

//    public static void main(String[] args) {
//        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//    }


}
